import http from '../utils/request';
import { useAuthStore } from '../store/auth';

// 用户API前缀
const USER_API_PREFIX = '[User]';

// 角色类型定义
export interface Role {
  id: number;
  code: string;
  name: string;
  description?: string;
  enable: boolean;
  permissions?: Permission[];
}

// 创建角色数据
export interface CreateRoleData {
  code: string;
  name: string;
  description?: string;
  enable?: boolean;
}

// 更新角色数据
export interface UpdateRoleData {
  code?: string;
  name?: string;
  description?: string;
  enable?: boolean;
}

// 权限类型（从permission.ts导入）
export interface Permission {
  id: number;
  code: string;
  name: string;
  type: string;
  path?: string;
  icon?: string;
  order?: number;
  enable: boolean;
  show: boolean;
  parentId?: number;
  children?: Permission[];
}

// 用户类型定义
export interface User {
  id: number;
  username: string;
  enable: boolean;
  createTime: string;
  updateTime: string;
  profile?: {
    nickName?: string;
    gender?: number;
    avatar?: string;
    address?: string;
    email?: string;
  };
  roles: Role[];
}

// 查询参数接口
export interface UserQueryParams {
  pageSize?: number;
  pageNo?: number;
  username?: string;
  gender?: number;
  role?: string;
  enable?: boolean;
}

// 创建用户接口
export interface CreateUserData {
  username: string;
  password: string;
  enable?: boolean;
  profile?: {
    nickName?: string;
    gender?: number;
    avatar?: string;
    address?: string;
    email?: string;
  };
  roleIds?: number[];
}

// 更新用户接口
export interface UpdateUserData {
  enable?: boolean;
  profile?: {
    nickName?: string;
    gender?: number;
    avatar?: string;
    address?: string;
    email?: string;
  };
  roleIds?: number[];
}

/**
 * 用户API
 */
export const userApi = {
  /**
   * 获取用户列表
   * @param {UserQueryParams} params - 查询参数
   * @returns {Promise} - HTTP GET请求的Promise对象
   */
  getUserList: (params: UserQueryParams = {}) => {
    console.log(`${USER_API_PREFIX} 获取用户列表`, params);
    // 使用完整参数，确保includeRoles=true以获取角色信息
    const fullParams = {
      ...params,
      includeRoles: true
    };
    return http.get('/user', fullParams, false, true, 'SUPER_ADMIN');
  },
  
  /**
   * 获取用户详情
   * @param {number} id - 用户ID
   * @returns {Promise} - HTTP GET请求的Promise对象
   */
  getUserDetail: (id: number) => {
    console.log(`${USER_API_PREFIX} 获取用户详情:`, id);
    return http.get(`/user/${id}`, {}, false, true, 'SUPER_ADMIN');
  },
  
  /**
   * 创建用户
   * @param {CreateUserData} data - 用户数据
   * @returns {Promise} - HTTP POST请求的Promise对象
   */
  createUser: (data: CreateUserData) => {
    console.log(`${USER_API_PREFIX} 创建用户`);
    return http.post('/user', data, false, true, true, 'SUPER_ADMIN');
  },
  
  /**
   * 更新用户
   * @param {number} id - 用户ID
   * @param {UpdateUserData} data - 用户数据
   * @returns {Promise} - HTTP PATCH请求的Promise对象
   */
  updateUser: (id: number, data: UpdateUserData) => {
    console.log(`${USER_API_PREFIX} 更新用户:`, id);
    return http.patch(`/user/${id}`, data, false, true, 'SUPER_ADMIN');
  },
  
  /**
   * 删除用户
   * @param {number} id - 用户ID
   * @returns {Promise} - HTTP DELETE请求的Promise对象
   */
  deleteUser: (id: number) => {
    console.log(`${USER_API_PREFIX} 删除用户:`, id);
    return http.delete(`/user/${id}`, {}, false, true, 'SUPER_ADMIN');
  },
  
  /**
   * 更新用户状态
   * @param {number} id - 用户ID
   * @param {boolean} enable - 启用状态
   * @returns {Promise} - HTTP PATCH请求的Promise对象
   */
  updateUserStatus: (id: number, enable: boolean) => {
    console.log(`${USER_API_PREFIX} 更新用户状态:`, id, enable);
    return http.patch(`/user/${id}`, { enable }, false, true, 'SUPER_ADMIN');
  },
  
  /**
   * 获取角色列表
   * @returns {Promise} - HTTP GET请求的Promise对象
   */
  getRoleList: () => {
    console.log(`${USER_API_PREFIX} 获取角色列表`);
    // 使用 auth-center 认证，并指定 SUPER_ADMIN 角色
    return http.get('/role', {}, false, true, 'SUPER_ADMIN');
  },
  
  /**
   * 添加用户角色
   * @param {number} userId - 用户ID
   * @param {number[]} roleIds - 角色ID数组
   * @returns {Promise} - HTTP POST请求的Promise对象
   */
  addUserRoles: (userId: number, roleIds: number[]) => {
    console.log(`${USER_API_PREFIX} 添加用户角色:`, userId, roleIds);
    return http.post(`/user/roles/${userId}`, { roleIds }, false, true, true, 'SUPER_ADMIN');
  },
  
  /**
   * 更新用户密码
   * @param {number} id - 用户ID
   * @param {string} password - 新密码
   * @returns {Promise} - HTTP PATCH请求的Promise对象
   */
  updateUserPassword: (id: number, password: string) => {
    console.log(`${USER_API_PREFIX} 更新用户密码:`, id);
    return http.patch(`/user/password/${id}`, { password }, false, true, 'SUPER_ADMIN');
  },

  /**
   * 创建角色
   * @param {CreateRoleData} data - 角色数据
   * @returns {Promise} - HTTP POST请求的Promise对象
   */
  createRole: (data: CreateRoleData) => {
    console.log(`${USER_API_PREFIX} 创建角色:`, data);
    return http.post('/role', data, false, true, true, 'SUPER_ADMIN');
  },

  /**
   * 更新角色
   * @param {number} id - 角色ID
   * @param {UpdateRoleData} data - 角色数据
   * @returns {Promise} - HTTP PUT请求的Promise对象
   */
  updateRole: (id: number, data: UpdateRoleData) => {
    console.log(`${USER_API_PREFIX} 更新角色:`, id, data);
    return http.patch(`/role/${id}`, data, false, true, 'SUPER_ADMIN');
  },

  /**
   * 删除角色
   * @param {number} id - 角色ID
   * @returns {Promise} - HTTP DELETE请求的Promise对象
   */
  deleteRole: (id: number) => {
    console.log(`${USER_API_PREFIX} 删除角色:`, id);
    return http.delete(`/role/${id}`, {}, false, true, 'SUPER_ADMIN');
  },

  /**
   * 获取角色权限
   * @param {number} roleId - 角色ID
   * @returns {Promise} - HTTP GET请求的Promise对象
   */
  getRolePermissions: (roleId: number) => {
    console.log(`${USER_API_PREFIX} 获取角色权限:`, roleId);
    return http.get(`/role/permissions?id=${roleId}`, {}, false, true, 'SUPER_ADMIN');
  },

  /**
   * 更新角色权限
   * @param {number} roleId - 角色ID
   * @param {number[]} permissionIds - 权限ID数组
   * @returns {Promise} - HTTP PUT请求的Promise对象
   */
  updateRolePermissions: (roleId: number, permissionIds: number[]) => {
    console.log(`${USER_API_PREFIX} 更新角色权限:`, roleId, permissionIds);
    return http.post('/role/permissions/add', { id: roleId, permissionIds }, false, true, true, 'SUPER_ADMIN');
  }
}; 